Conversions
C# allows you to create a special type of operator method called a conversion operator. A conversion operator converts an object of your class into another type. There are two forms of conversion operators, implicit and explicit.
The conversion operator specifies implicit, then the conversion is invoked automatically, such as when an object is used in an expression with the target type. When the conversion operator specifies explicit, the conversion is invoked when a cast is used. You cannot define both an implicit and explicit conversion operator for the same target and source types. (I.e. in the same class).

public static operator implicit target-type(source-type v) { return value; }
public static operator explicit target-type(source-type v) { return value; }

 

 

program on implicit conversion operator

using System;
class abc
{
public int a, b;
public void get(int x, int y)
{
a = x;
b = y;

    }
public static abc operator +(abc x, abc y)
{
abc k = new abc();
k.a = x.a + y.a;
k.b = x.b + y.b;
return k;
}
public static implicit operator int (abc x)
{
int k;
k=x.a*x.b;
return k;
}
public void put()
{
Console.WriteLine("Sum of 2nos{0}", (a + b));
}

}

class sample
{
public static void Main()
{
abc x = new abc();
abc y = new abc();

        x.get(6, 8);
x.put();
y.get(5, 2);
y.put();
abc z;
z = x + y;
z.put();

int c;
c=z;
Console.WriteLine ("Values of c{0}",c);
c = z * 2;
Console.WriteLine("Value of z*2  {0}", c);
c = z * 2 - x;
Console.WriteLine("Valie of Z*2-x  {0}", c);

    }
}

Program on Explicit conversions

using System;
class abc
{
public int a, b;
public void get(int x, int y)
{
a = x;
b = y;

    }
public static abc operator +(abc x, abc y)
{
abc k = new abc();
k.a = x.a + y.a;
k.b = x.b + y.b;
return k;
}
public static explicit operator int (abc x)
{
int k;
k=x.a*x.b;
return k;
}
public void put()
{
Console.WriteLine("Sum of 2nos{0}", (a + b));
}

}

class sample
{
public static void Main()
{
abc x = new abc();
abc y = new abc();

        x.get(6, 8);
x.put();
y.get(5, 2);
y.put();
abc z;
z = x + y;
z.put();

int c;
c=(int)z;
Console.WriteLine ("Values of c{0}",c);
c = (int)z * 2;
Console.WriteLine("Value of z*2  {0}", c);
c = (int)z * 2 - (int)x;
Console.WriteLine("Valie of Z*2-x  {0}", c);

    }
}